package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hangxiang.approval.common.*;
import com.hangxiang.approval.dao.ActiongroupDao;
import com.hangxiang.approval.dao.CcPersonDao;
import com.hangxiang.approval.dao.ReviewerDao;
import com.hangxiang.approval.dao.TemplateDao;
import com.hangxiang.approval.entity.*;
import com.hangxiang.approval.enums.ResultStatusEnum;
import com.hangxiang.approval.exception.MyException;
import com.hangxiang.approval.service.TemplateService;
import com.hangxiang.approval.utils.BeanConvertUtils;
import com.hangxiang.approval.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * (Template)表服务实现类
 *
 * @author makejava
 * @since 2020-11-23 18:07:07
 */
@Service("templateService")
public class TemplateServiceImpl extends ServiceImpl<TemplateDao, Template> implements TemplateService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TemplateDao templateDao;

    @Resource
    private ActiongroupDao actiongroupDao;

    @Resource
    private ReviewerDao reviewerDao;

    @Resource
    private CcPersonDao ccPersonDao;
    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public TemplateVo queryById(IntegerCommon common) {
        TemplateVo vo = new TemplateVo();
        Template template = templateDao.selectById(common.getValue());
        BeanUtils.copyProperties(template, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param templateInsertCommon 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public Integer insertTemplate(TemplateInsertCommon templateInsertCommon) {
        Template template = new Template();
        BeanUtils.copyProperties(templateInsertCommon, template);
        template.setCreateTime(new Date());
        template.setUpdateTime(new Date());
        templateDao.insert(template);

        Actiongroup actiongroup=new Actiongroup();
        actiongroup.setActionName(templateInsertCommon.getTempName());
        actiongroup.setTempId(template.getTempId());
        actiongroupDao.insert(actiongroup);

//        List<Reviewer> reviewerList=templateInsertCommon.getReviewerList();
//        Long priCount  = Long.valueOf(reviewerList.size());   //集合的长度
//        Long nextCount =  reviewerList.stream().distinct().count();
//        if(nextCount!=priCount){
//            throw new MyException(ResultStatusEnum.REPEAT_REVIEWER);
//        }
//        reviewerList.forEach(reviewers -> {
//            Reviewer reviewer= new Reviewer();
//            reviewer.setId(reviewers.getId());
//            reviewer.setReviewerName(reviewers.getReviewerName());
//            reviewer.setCreateTime(new Date());
//            reviewer.setActionGroupId(actiongroup.getActionId());
//            reviewerDao.insert(reviewer);
//        });
//
//
//        List<CcPerson> ccPersonList=templateInsertCommon.getPersonList();
//        ccPersonList.forEach(ccPersons -> {
//            CcPerson ccPerson= new CcPerson();
//            ccPerson.setCcPersonId(ccPersons.getCcPersonId());
//            ccPerson.setReviewerId(ccPersons.getReviewerId());
//            ccPerson.setCreateTime(new Date());
//            ccPerson.setActionGroupId(actiongroup.getActionId());
//            ccPersonDao.insert(ccPerson);
//        });

        return template.getTempId();
    }

    /**
     * 修改数据
     *
     * @param templateUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    public Integer updateTemplate(TemplateUpdateCommon templateUpdateCommon) {
        Template template = new Template();
        BeanUtils.copyProperties(templateUpdateCommon, template);
        templateDao.updateByTempId(template);
        return template.getTempId();
    }

    @Override
    public PageInfo selectAllInfo(TemplatePageCommon common) {
        PageHelper.startPage(common.getPageRow(),common.getPageSize());
        QueryWrapper<Template> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Template::getIsDeleted,0);
        if(common.getTempName()!=null){
            queryWrapper.lambda().like(Template::getTempName,common.getTempName());
        }
        if(common.getTypeId()!=null){
            queryWrapper.lambda().eq(Template::getTypeId,common.getTypeId());
        }
        List<Template> templateList=templateDao.selectList(queryWrapper);
        List<TemplateVo> templateVoList = null;
        if (templateList != null && templateList.size() >0){
            templateVoList = BeanConvertUtils.convertListTo(templateList, TemplateVo::new);
            templateVoList.forEach(template -> {
                List<ReviewerVo> reviewerList=reviewerDao.selectListCC(template.getTempId());
                template.setReviewerVoList(reviewerList);
            });
        }
        PageInfo info = new PageInfo(templateVoList);
        return info;
    }

    @Override
    public TemplateVo selectById(TemplateCommon common) {
        TemplateVo templateVo=new TemplateVo();
        //查询模板名称
        Template template = templateDao.selectById(common.getTempId());
        templateVo.setTempId(template.getTempId());
        templateVo.setTempName(template.getTempName());
        //查询流程id
        Actiongroup actiongroup=actiongroupDao.selectByTempId(template.getTempId());
        List<Reviewer> reviewers=reviewerDao.selectList(new QueryWrapper<Reviewer>().lambda().eq(Reviewer::getActionGroupId,actiongroup.getActionId()));
        List<ReviewerVo> reviewerVoList = BeanConvertUtils.convertListTo(reviewers, ReviewerVo::new);
        templateVo.setReviewerVoList(reviewerVoList);
        reviewerVoList.forEach(reviewer -> {
            //查询抄送人信息
            List<CcPerson> ccPeoples=ccPersonDao.selectList(new QueryWrapper<CcPerson>().lambda().eq(CcPerson::getReviewerId,reviewer.getId()));
            List<CcPersonVo> ccPersonVoList=BeanConvertUtils.convertListTo(ccPeoples,CcPersonVo::new);
            reviewer.setCcPersonVoList(ccPersonVoList);
        });
        return templateVo;
    }

    @Override
    public List<ReviewerVo> findTempInfo(IntegerCommon common) {
        ActiongroupVo vo = new ActiongroupVo();
        Actiongroup actiongroup = actiongroupDao.selectByTempId(common.getValue());
        if (actiongroup == null){
            return new ArrayList<>();
        }
        BeanUtils.copyProperties(actiongroup,vo);
        List<Reviewer> reviewers=reviewerDao.selectList(new QueryWrapper<Reviewer>().lambda().eq(Reviewer::getActionGroupId,actiongroup.getActionId()));

        List<ReviewerVo> reviewerVos=BeanConvertUtils.convertListTo(reviewers,ReviewerVo::new);
        if (reviewers == null || reviewers.size() == 0){
            throw new MyException(ResultStatusEnum.TEMP_DATA_ERR);
        }
        reviewerVos.forEach(reviewer -> {
            List<CcPerson> ccPeoples=ccPersonDao.selectList(new QueryWrapper<CcPerson>().lambda().eq(CcPerson::getReviewerId,reviewer.getId()));
            List<CcPersonVo> ccPersonVos=BeanConvertUtils.convertListTo(ccPeoples,CcPersonVo::new);
            reviewer.setCcPersonVoList(ccPersonVos);
        });

        return reviewerVos;
    }


}